package com.obj;

import com.GameWin;
import com.com.utils.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class HeadObj extends GameObj {

    //定义变量，控制方向
    private String direction = "right";

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public HeadObj(Image img, int x, int y, GameWin frame) {
        super(img, x, y, frame);

        //键盘监听事件
        this.frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }

    //控制方向  w a s d
    public void changeDirection(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                if (!"right".equals(direction)) {
                    direction = "left";
                    img = GameUtils.leftImg;
                }
                break;
            case KeyEvent.VK_D:
                if (!"left".equals(direction)) {
                    direction = "right";
                    img = GameUtils.rightImg;
                }
                break;
            case KeyEvent.VK_W:
                if (!"down".equals(direction)) {
                    direction = "up";
                    img = GameUtils.upImg;
                }
                break;
            case KeyEvent.VK_S:
                if (!"up".equals(direction)) {
                    direction = "down";
                    img = GameUtils.downImg;
                }
                break;
            default:
                break;
        }

    }

    public void move() {
        //蛇身的移动
        java.util.List<BodyObj> bodyObjList = this.frame.bodyObjList;
        for (int i = bodyObjList.size() - 1; i >= 1; i--) {
            bodyObjList.get(i).x = bodyObjList.get(i - 1).x;
            bodyObjList.get(i).y = bodyObjList.get(i - 1).y;
            if (this.x == bodyObjList.get(i).x && this.y == bodyObjList.get(i).y) {
                GameWin.state = 3;//游戏失败
            }
        }
        bodyObjList.get(0).x = this.x;
        bodyObjList.get(0).y = this.y;

        //蛇头的移动
        switch (direction) {
            case "up":
                y -= height;
                break;
            case "down":
                y += height;
                break;
            case "left":
                x -= width;
                break;
            case "right":
                x += width;
                break;
            default:
                break;
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        //蛇吃食物食物
        FoodObj food = this.frame.foodObj;
        // 蛇身的最后一节
        Integer newX = null;
        Integer newY = null;
        if (this.x == food.x && this.y == food.y) {
            this.frame.foodObj = food.getFood();  //吃了之后，食物随机生成
            //获取蛇身的最后一节
            BodyObj lastBody = this.frame.bodyObjList.get(this.frame.bodyObjList.size() - 1);
            newX = lastBody.x;
            newY = lastBody.y;
            //分数+1
            this.frame.score++;
        }
        //加速
        if (this.frame.score<10){
            GameWin.run=200;
        }
        else if (this.frame.score>=10){
            GameWin.run=100;
        }else if (this.frame.score>=20){
            GameWin.run=50;
        }
        //通关
        if (this.frame.score >= 30) {
            GameWin.state = 4;//通关，分数大于15
        }
        //蛇移动
        move();

        if (newX != null && newY != null) {
            this.frame.bodyObjList.add(new BodyObj(GameUtils.bodyImg, newX, newY, this.frame));
        }


        //越界处理
        if (x < 0) {
            x = 570;
        } else if (x > 570) {
            x = 0;
        } else if (y < 30) {
            y = 570;
        } else if (y > 570) {
            y = 30;
        }
    }
}
